package com.hts.spring06.utils;

import lombok.extern.java.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Logger;

public class DynamicProxy {

    static Logger logger = Logger.getLogger("DynamicProxy");

    public static Object proxy(Object target){

//        InvocationHandler aa= new InvocationHandler(){
//
//            @Override
//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                return null;
//            }
//        };
//
//        InvocationHandler aa1= ( proxy,  method,  args)->{
//            return null;
//        };

        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                ( proxy,  method,  args)-> {
                    logger.info(method.getName()+"开始执行");
                    logger.info(method.getName()+"参数:" + args[0]);
                    Object result =null;
                    try {
                        result = method.invoke(target, args);
                        logger.info("执行完毕, 结果为:" + result);
                    }catch (Exception e){
                        e.printStackTrace();
                        logger.info("有异常");
                    }

                    return result;
                });


    }
}


//class AA implements InvocationHandler{
//
//    @Override
//    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        return null;
//    }
//}